package com.yswy.theater.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yswy.theater.common.BasePojo;
import com.yswy.theater.entity.TheaterHotSearch;
import com.yswy.theater.entity.TheaterPlay;
import com.yswy.theater.mapper.TheaterHotSearchMapper;
import com.yswy.theater.service.TheaterHotSearchService;
import com.yswy.theater.service.TheaterPlayService;
import com.yswy.theater.vo.res.TheaterPlayResVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;


@Slf4j
@Service
public class TheaterHotSearchServiceImpl extends ServiceImpl<TheaterHotSearchMapper, TheaterHotSearch>
        implements TheaterHotSearchService {
    @Autowired
    private TheaterPlayService theaterPlayService;

    @Override
    public List<String> hotSearch() {
        List<String> list = new ArrayList<>();
        List<TheaterHotSearch> theaterHotSearches =  list(new LambdaQueryWrapper<TheaterHotSearch>()
                .orderByDesc(TheaterHotSearch::getSortId).last("limit 10"));
        for (TheaterHotSearch t:theaterHotSearches) {
            list.add(t.getSearchName());
        }
        return list;
    }

    @Override
    public List<TheaterPlayResVo> search(String msg , Integer sex) {
        if (msg.length()>10){
            throw new IllegalArgumentException("服务器异常");
        }
        LambdaQueryWrapper<TheaterPlay> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<String> msgs = splitKeyWord(msg);
        msgs.add(msg);
        System.out.println(msgs);
        for (int i = 0; i < msgs.size(); i++) {
            if (i+1 ==msgs.size()){
                lambdaQueryWrapper.like(TheaterPlay::getPlayName , msgs.get(i));
            }else {
                lambdaQueryWrapper.like(TheaterPlay::getPlayName , msgs.get(i)).or();
            }
        }
        List<TheaterPlay> list = theaterPlayService.list(lambdaQueryWrapper
                        .eq(BasePojo::getIsDel,0)
                        .eq(TheaterPlay::getIsShow , 1)
                        .orderByDesc(BasePojo::getCreateTime)
                        .in(TheaterPlay::getSex , 0 ,sex ));



        List<TheaterPlayResVo> theaterPlayResVos = TheaterPlayResVo.getList(list );

        TheaterPlayResVo theaterPlayResVoOne = null ;
        for (TheaterPlayResVo theaterPlayResVo:theaterPlayResVos) {
            if (theaterPlayResVo.getPlayName().equals(msg)){
                theaterPlayResVoOne = theaterPlayResVo;
            }
        }
        if (theaterPlayResVoOne != null){
            List<TheaterPlayResVo> theaterPlayResVosreturn = new ArrayList<>();
            theaterPlayResVosreturn.add(theaterPlayResVoOne);
            for (TheaterPlayResVo theaterPlayResVo:theaterPlayResVos) {
                if (!theaterPlayResVo.getPlayName().equals(msg)){
                    theaterPlayResVosreturn.add(theaterPlayResVo);
                }
            }
            return theaterPlayResVosreturn;
        }else {
            return  theaterPlayResVos ;
        }
    }

    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        String text = "战神逆天纪";

//        String analysisedText = ToAnalysis.parse(text).toStringWithOutNature();
        List<String> list = splitKeyWord(text);
        long endTime = System.currentTimeMillis();
        long time = endTime - startTime;
        System.out.println("精准分词: " + list.toString() + "(" + time + "ms)");
    }


    /**
     *  分词
     * @param keyword 需要分词的文本
     * @return
     */
    public static List<String> splitKeyWord(String keyword) {
        ArrayList<String> result = new ArrayList<>();
        try {
            // 创建一个reader对象
            StringReader reader = new StringReader(keyword);
            // 创建一个分词对象
            IKSegmenter ikSegmenter = new IKSegmenter(reader, false);
            Lexeme next = ikSegmenter.next();

            while ( next != null ) {
                // 获取分词的结果
                result.add(next.getLexemeText());
                next = ikSegmenter.next();
            }
        }catch (Exception e){
            e.printStackTrace();
        }


        return result;
    }
}
